import os
import json
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import make_interp_spline

from matplotlib.pyplot import MultipleLocator
from year_data import get_every_year_avg

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# ys = [{'year_avg': [0.025674129152398167, 0.024608728007784218, 0.02576023918248887, 0.02547847420168763,
#                     0.025635186178712053, 0.024716121620194577, 0.025005145236121674, 0.027399386275585726,
#                     0.02651119728458503, 0.027216223527940855, 0.0269204741873533, 0.026142068882436108,
#                     0.028468990349317717, 0.02849322809421447, 0.029630292692936416, 0.03066358334341028,
#                     0.030884933714201375, 0.033029005502176204, 0.030453776530719966, 0.029479680587368627,
#                     0.0339355218027718],
#        'growth_avg': [0.02945063246592557, 0.02967839842493621, 0.030595515615268718, 0.033233559781560035,
#                       0.03173111638691142, 0.03287274697628158, 0.031255313575571216, 0.03323475151127093,
#                       0.03404173715898724, 0.03381271379688503, 0.035230335585462465, 0.03345995039934863,
#                       0.03506127478515234, 0.03497417113841994, 0.03606125518834786, 0.03741825194286583,
#                       0.036449104490104904, 0.039223680395699866, 0.03724405853320591, 0.036729248247075005,
#                       0.039438161058147465]},
#       {'year_avg': [0.12886686683259127, 0.12259952261767026, 0.1212927075142278, 0.12622560750567535,
#                     0.1267718064921181, 0.12488795669031051, 0.13117912226135542, 0.1380507867471381,
#                     0.13351965879869085, 0.14012666926161013, 0.13202176885000141, 0.13742586542031293,
#                     0.1419790446722534, 0.1441797608994483, 0.14784108774455842, 0.15134892207298456,
#                     0.15299477264278058, 0.15692624141547487, 0.14453222526861362, 0.1430807860207258,
#                     0.15652755895222933],
#        'growth_avg': [0.152662805744296, 0.1564517304173288, 0.14729035340732458, 0.1610168415035425,
#                       0.15574523403806445, 0.1623285998775294, 0.16214285585799318, 0.16671402999178264,
#                       0.16667904309438208, 0.16931895539927308, 0.1656364372746521, 0.17359957170250617,
#                       0.1718110318558264, 0.17401952051952094, 0.172492366891997, 0.17986795026694932,
#                       0.17706883732246406, 0.18463072152257246, 0.17006728969217835, 0.1761421870935404,
#                       0.17767137193216245]},
#       {'year_avg': [0.31882859158926175, 0.31343435484048576, 0.3039013597528884, 0.3083653060486516, 0.326680142683289,
#                     0.3080154231924346, 0.3221389763095884, 0.3350222375481797, 0.3315433620771069, 0.34148669246036095,
#                     0.3186021073847978, 0.33300660225240003, 0.33878480725027926, 0.35885722157979516,
#                     0.3522743691194112, 0.3697696936630251, 0.3618515454909053, 0.39766098863886457,
#                     0.36405164411284724, 0.3513819217666856, 0.3599158518512105],
#        'growth_avg': [0.3636202443542906, 0.3833169153300906, 0.3552239653482586, 0.3834852695099753,
#                       0.39538558611347213, 0.39729043621082, 0.38831369021841855, 0.3951234991054974,
#                       0.4060345403528832, 0.41088530451082705, 0.39110823697065694, 0.40982611053748624,
#                       0.4080712656293843, 0.44033838007065473, 0.4128139159953561, 0.44520627665102663,
#                       0.4115363660560534, 0.4570705503772648, 0.4419504548664049, 0.43600026859312685,
#                       0.416203071201692]},
#       {'year_avg': [0.061115228194478545, 0.05839869714698098, 0.06081513184837811, 0.057979632289391,
#                     0.0632822066228238, 0.0578795076630448, 0.060295942833375464, 0.06523646479945722,
#                     0.0619055083520398, 0.06210333665108821, 0.05874893588296557, 0.06129526215005558,
#                     0.06512765429574448, 0.06810297402875574, 0.06911063027049268, 0.07223371370923637,
#                     0.07250315333124145, 0.07745288789357023, 0.06954332727736756, 0.06909377094564938,
#                     0.07284572858504024],
#        'growth_avg': [0.0722335944748492, 0.07426123124376442, 0.07463910250216019, 0.07681782719099822,
#                       0.08052633362345207, 0.0808636518963822, 0.07860427374914122, 0.08249700050662384,
#                       0.0802983984431542, 0.08008295225156006, 0.07946077782473518, 0.08066609097978403,
#                       0.08427900616011096, 0.08902060700548303, 0.08483469579393423, 0.0916330680838073,
#                       0.08688681996272322, 0.0938183391392619, 0.09078979698225806, 0.09134591739683648,
#                       0.08798953664710048]},
#       {'year_avg': [0.06604482503517072, 0.0628885498660137, 0.06375924567986263, 0.06228336908087242,
#                     0.06549543379373944, 0.06266993230447129, 0.06321418979846291, 0.0696335805096172,
#                     0.06700116211672796, 0.06660850293335077, 0.06545336758194996, 0.0645202217150601,
#                     0.06926333681387457, 0.06760037956971597, 0.06866050987935768, 0.0727767228295763,
#                     0.07042359235246226, 0.07646373220577767, 0.06858238896063726, 0.06565039499706375,
#                     0.0733850577592888],
#        'growth_avg': [0.07781028565389372, 0.07939590921274126, 0.07836671227439915, 0.08348903854522595,
#                       0.08404088907259408, 0.08795997052094721, 0.08168879849641807, 0.08807086890756602,
#                       0.08887970973265845, 0.08719495469908407, 0.08936694974919998, 0.08726635387852426,
#                       0.09039842385783707, 0.08979845808324606, 0.09029145725017007, 0.09296251796427768,
#                       0.08725883796133194, 0.09654032594412101, 0.09008397833759574, 0.08689824753697528,
#                       0.08952509233199131]},
#       {'year_avg': [0.29012762922241425, 0.2836304688664958, 0.27241859146003533, 0.2684446778270825,
#                     0.28258706779112364, 0.26846766723142396, 0.2824555374904829, 0.302994626056933,
#                     0.28637393200621997, 0.2876537371569472, 0.2623677255719375, 0.2762332958627464, 0.288527322717075,
#                     0.2944789634612185, 0.2993650103890501, 0.3032408710821218, 0.2944962236506435, 0.3263937068260101,
#                     0.3022449056789901, 0.2922434186340249, 0.29365361515768723],
#        'growth_avg': [0.33475044629518885, 0.355866261557462, 0.3303544546581626, 0.3427149254358985,
#                       0.3604834567844592, 0.3639016309116619, 0.3572707968815096, 0.3780225226542362,
#                       0.3663490941911821, 0.36298337782605206, 0.3412606772508507, 0.35733788443104153,
#                       0.3674929243414567, 0.3771100166196414, 0.3662113883423231, 0.38018897168249965,
#                       0.3453452100929332, 0.390556542470108, 0.3829059099674319, 0.3826324369546868,
#                       0.3547409097224629]}]


with open("avgRecord.json") as f:
    data = json.load(f)
    d1 = data['QuNDVI_Excel']


def demo():
    with open("avgRecord.json") as f:
        data = json.load(f)
        d1 = data['QuNDVI_Excel']
        d2 = d1.values()
        print(list(d2))
        return list(d2)


# ys = demo()



def draw(title, fig, params, py):
    x = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013',
         '2014', '2015', '2016', '2017', '2018', '2019', '2020']
    xx = np.arange(0, len(x), 1)
    xn = np.array(xx)
    x_smooth = np.linspace(xn.min(), xn.max(), 300)
    y = py
    print(y)
    y_year_avg = y['year_avg']
    y_growth_avg = y['growth_avg']
    y_year_avg_smooth = make_interp_spline(xn, y_year_avg)(x_smooth)
    y_growth_avg_smooth = make_interp_spline(xn, y_growth_avg)(x_smooth)
    plt__ = fig.add_subplot(params)
    plt__.plot(x_smooth, y_year_avg_smooth, label="NDVI年度平均变化趋势图")
    plt__.plot(x_smooth, y_growth_avg_smooth, label="NDVI生长期（4-10月）平均变化趋势图")
    plt__.set_title("%sNDVI变化趋势图" % os.path.basename(title))
    # plt__.set_xticklabels(x)
    plt__.legend(loc=2)
    plt__.grid(alpha=0.1, color='g', which="major")


def run():
    root_dir = r"E:\毕设数据\UsingTypeNDVI_Excel"
    dirs = os.listdir(root_dir)
    c = 721
    fig = plt.figure(figsize=(30, 90))

    x = list(d1)
    print(x[:6])
    print(x[6:])
    for i in x[:6]:
        print(i)
        print(d1[i])
        draw(title=i, fig=fig, params=c, py=d1[i])
        c +=1

        t = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013',
             '2014', '2015', '2016', '2017', '2018', '2019', '2020']
        plt.xticks(np.arange(0, len(t), 1),t, rotation=30)

        plt.tight_layout(pad=8,  w_pad=1.5, h_pad=8)
    plt.savefig("E:\\毕设数据\\pictures\\test2.pdf")
    #
    # for i in range(len(dirs)):
    #     d = os.path.join(root_dir, dirs[i])
    #     draw(d, fig, c, ys[i])
    #
    #     c +=1
    #
    #     t = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013',
    #          '2014', '2015', '2016', '2017', '2018', '2019', '2020']
    #     plt.xticks(np.arange(0, len(t), 1),t, rotation=30)
    #
    #     plt.tight_layout(pad=8,  w_pad=1.5, h_pad=8)
    # plt.savefig("E:\\毕设数据\\pictures\\test.pdf")

if __name__ == '__main__':
    run()
    # demo()